package com.oazon.moco.serialport;

import java.util.ArrayList;
import java.util.List;

public class SerialControll {

	private static final String TAG = "SerialControll";

	private SerialControll() {
		init();
	};

	private static SerialControll object = null;

	public static synchronized SerialControll getInstance() {
		if (object == null) {
			object = new SerialControll();
		}
		return object;
	}

	private List<Cmd> cmds = new ArrayList<SerialControll.Cmd>();
	private List<Byte> controls = new ArrayList<Byte>();
	private List<Byte> advances = new ArrayList<Byte>();

	private void init() {
		initCmd();
		initCmdClass();
	}

	private void initCmd() {
		cmds.add(new Cmd(SerialHandlerService.MCU_BAT_CHARGING_STATUS, 0));
		cmds.add(new Cmd(SerialHandlerService.MCU_BAT_FULL_STATUS, 0));
		cmds.add(new Cmd(SerialHandlerService.CMD_RTN_BAT_LEVEL, 0));
		cmds.add(new Cmd(SerialHandlerService.CMD_RTN_IS_DC_IN, 0));
		cmds.add(new Cmd(SerialHandlerService.CMD_RTN_IS_BAT_CHARGING, 0));
		//cmds.add(new Cmd(SerialHandlerService.M_VOLUM_FIND, 0));
		cmds.add(new Cmd(SerialHandlerService.M_FIND_S_SATATE, 0));
		cmds.add(new Cmd(SerialHandlerService.MCU_KEY_VOL_UP, 0));
		cmds.add(new Cmd(SerialHandlerService.MCU_KEY_VOL_DOWN, 0));
		cmds.add(new Cmd(SerialHandlerService.MCU_KEY_PLAYPAUSE, 0));
		cmds.add(new Cmd(SerialHandlerService.MCU_KEY_TOP, 0));
		cmds.add(new Cmd(SerialHandlerService.MCU_KEY_PREV, 0));
		cmds.add(new Cmd(SerialHandlerService.MCU_KEY_NEXT, 0));
		cmds.add(new Cmd(SerialHandlerService.MCU_KEY_VOICE_CMD_PRESSED, 0));
		cmds.add(new Cmd(SerialHandlerService.MCU_KEY_VOICE_CMD_RELEASED, 0));
		cmds.add(new Cmd(SerialHandlerService.CMD_RTN_VERSION, 0));
		cmds.add(new Cmd(SerialHandlerService.CMD_RTN_VERSION,0));
	}

	private void initCmdClass() {
		// 控制类型命令
		controls.add(SerialHandlerService.MCU_KEY_VOL_UP);
		controls.add(SerialHandlerService.MCU_KEY_VOL_DOWN);
		controls.add(SerialHandlerService.MCU_KEY_PLAYPAUSE);
		controls.add(SerialHandlerService.MCU_KEY_PREV);
		controls.add(SerialHandlerService.MCU_KEY_NEXT);
		controls.add(SerialHandlerService.MCU_KEY_VOICE_CMD_PRESSED);
		controls.add(SerialHandlerService.MCU_KEY_VOICE_CMD_RELEASED);
		// 高级命令
		advances.add(SerialHandlerService.MCU_KEY_PREV);
		advances.add(SerialHandlerService.MCU_KEY_NEXT);
	}

	public void stopAll() {
		countOpera(null, true);
	}

	public void startAll() {
		countOpera(null, false);
	}

	public void fullStartAll() {
		for (Cmd cmd : cmds) {
			cmd.setCount(0);
		}
	}

	public void stopControl() {
		countOpera(controls, true);
	}

	public void startControl() {
		countOpera(controls, false);
	}

	public boolean isCanPowerChange() {
		if (getCmdStopCount(SerialHandlerService.MCU_BAT_CHARGING_STATUS) <= 0) {
			return true;
		}
		return false;
	}

	public boolean isCanPowerFull() {
		if (getCmdStopCount(SerialHandlerService.MCU_BAT_FULL_STATUS) <= 0) {
			return true;
		}
		return false;
	}

	public boolean isCanPowerPercent() {
		if (getCmdStopCount(SerialHandlerService.CMD_RTN_BAT_LEVEL) <= 0) {
			return true;
		}
		return false;
	}

	public boolean isCanDCIn() {
		if (getCmdStopCount(SerialHandlerService.CMD_RTN_IS_DC_IN) <= 0) {
			return true;
		}
		return false;
	}

	public boolean isCanBatCharging() {
		if (getCmdStopCount(SerialHandlerService.CMD_RTN_IS_BAT_CHARGING) <= 0) {
			return true;
		}
		return false;
	}

	/*public boolean isCanVolFind() {
		if (getCmdStopCount(SerialHandlerService.M_VOLUM_FIND) <= 0) {
			return true;
		}
		return false;
	}*/

	public boolean isCanFindSState() {
		if (getCmdStopCount(SerialHandlerService.M_FIND_S_SATATE) <= 0) {
			return true;
		}
		return false;
	}

	public boolean isCanVolUp() {
		if (getCmdStopCount(SerialHandlerService.MCU_KEY_VOL_UP) <= 0) {
			return true;
		}
		return false;
	}

	public boolean isCanVolDown() {
		if (getCmdStopCount(SerialHandlerService.MCU_KEY_VOL_DOWN) <= 0) {
			return true;
		}
		return false;
	}

	public boolean isCanShift() {
		if (getCmdStopCount(SerialHandlerService.MCU_KEY_PLAYPAUSE) <= 0) {
			return true;
		}
		return false;
	}

	public boolean isCanSpeechHold() {
		if (getCmdStopCount(SerialHandlerService.MCU_KEY_TOP) <= 0) {
			return true;
		}
		return false;
	}

	public boolean isCanPrevious() {
		if (getCmdStopCount(SerialHandlerService.MCU_KEY_PREV) <= 0) {
			return true;
		}
		return false;
	}

	public boolean isCanNext() {
		if (getCmdStopCount(SerialHandlerService.MCU_KEY_NEXT) <= 0) {
			return true;
		}
		return false;
	}

	public boolean isCanSpeechStart() {
		if (getCmdStopCount(SerialHandlerService.MCU_KEY_VOICE_CMD_PRESSED) <= 0) {
			return true;
		}
		return false;
	}

	public boolean isCanSpeechEnd() {
		if (getCmdStopCount(SerialHandlerService.MCU_KEY_VOICE_CMD_RELEASED) <= 0) {
			return true;
		}
		return false;
	}

	public boolean isCanHardwareVersion() {
		if (getCmdStopCount(SerialHandlerService.CMD_RTN_VERSION) <= 0) {
			return true;
		}
		return false;
	}

	public boolean isCanRtnBatteryVoltage() {
		if (getCmdStopCount(SerialHandlerService.CMD_RTN_BATTERY_VOLTAGE) <= 0) {
			return true;
		}
		return false;
	}
	private int getCmdStopCount(Byte cmd) {
		for (Cmd temp : cmds) {
			if (temp.getKey() == cmd) {
				return temp.getCount();
			}
		}
		return -1;
	}

	private void countOpera(List<Byte> bytes, boolean isAdd) {
		if (bytes != null) {
			for (Cmd cmd : cmds) {
				for (Byte byte1 : bytes) {
					if (cmd.getKey() == byte1) {
						opera(cmd, isAdd);
						break;
					}
				}
			}
		} else {
			for (Cmd cmd : cmds) {
				opera(cmd, isAdd);
			}
		}
	}

	private void opera(Cmd cmd, boolean isAdd) {
		int count = cmd.getCount();
		if (isAdd) {
			count++;
		} else {
			count--;
		}
		cmd.setCount(count);
	}

	private class Cmd {

		private Byte key;
		private Integer count;

		public Cmd(Byte key, Integer count) {
			this.key = key;
			this.count = count;
		}

		public Byte getKey() {
			return key;
		}

		public void setKey(Byte key) {
			this.key = key;
		}

		public Integer getCount() {
			return count;
		}

		public void setCount(Integer count) {
			this.count = count;
		}

	}
}
